//
//  HSMessageCell.swift
//  HomeSchoolDemo
//
//  Created by Mac on 16/10/10.
//  Copyright © 2016年 chendong. All rights reserved.
//

import UIKit

//用于回调点赞和回复按钮事件的闭包类型
typealias CellBtnCallback = (_ cell:UITableViewCell) -> ()

class HSMessageCell: UITableViewCell {
    
    @IBOutlet weak var messageIconV: UIImageView!

    @IBOutlet weak var messageUserName: UILabel!
    
    @IBOutlet weak var messageType: UILabel!
    
    @IBOutlet weak var messageDate: UILabel!
    
    @IBOutlet weak var messageTitle: UILabel!
    
    @IBOutlet weak var messageContent: UILabel!
    
    @IBOutlet weak var likeButton: UIButton!
    
    @IBOutlet weak var messagereplayViewH: NSLayoutConstraint!
    
    
    //回复背景视图（使用了自定义类型）
    @IBOutlet weak var messageReplayBgV: HSReplayView!
    
    //点赞按钮的回调闭包
    var likeBtnCallback: CellBtnCallback?
    //回复按钮的回调闭包
    var replayBtnCallback: CellBtnCallback?
    
    //数据模型（消息视图模型）
    var messageVM: HSMessageViewModel? {
        didSet{
            messageUserName.text = messageVM?.message.not_Name
            messageType.text = messageVM?.message.not_courseName
            messageDate.text = messageVM?.message.not_createDate
            messageTitle.text = messageVM?.message.not_title
            messageContent.text = messageVM?.message.not_content
            //设置点赞按钮
            let likeN = messageVM?.message.likenum ?? 0
            if likeN > 0{
                likeButton.setTitle("\(likeN)", for: .selected)
                likeButton.isSelected = true
            }else{
                likeButton.isSelected = false
            }
            //传递模型给回复视图
            messageReplayBgV.messageVM = messageVM
            //设置高度
            messagereplayViewH.constant = messageVM?.replayViewH ?? 0
        }
    }
    
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        //设置圆角
        messageIconV.layer.cornerRadius = 25
        messageIconV.layer.masksToBounds = true
        
        //设置用户类型视图边框
        messageType.layer.cornerRadius = 8
        messageType.layer.borderWidth = 1
        messageType.layer.borderColor = UIColor().colorWithHex(0x00a0e9).cgColor
        
        
    }
    //点赞按钮监听事件回调
    @IBAction func likeBtnClicked(_ sender: UIButton) {
        print("点赞")
        likeBtnCallback?(self)
    }
    //回复按钮监听事件回调
    @IBAction func messageReplayBtnClicked(_ sender: UIButton) {
        print("回复")
        replayBtnCallback?(self)
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
    
}
